package com.ruoyi.ihome.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.ihome.domain.Children;
import com.ruoyi.ihome.domain.DistrictType;
import com.ruoyi.ihome.domain.Search;
import com.ruoyi.ihome.mapper.DistrictTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ihome.mapper.DistrictMapper;
import com.ruoyi.ihome.domain.District;
import com.ruoyi.ihome.service.IDistrictService;

import javax.annotation.Resource;

/**
 * 区域Service业务层处理
 *
 * @author mage
 * @date 2024-08-13
 */
@Service
public class DistrictServiceImpl implements IDistrictService {
    @Resource
    private DistrictMapper districtMapper;
    @Resource
    private DistrictTypeMapper districtTypeMapper;

    /**
     * 查询区域
     *
     * @param id 区域主键
     * @return 区域
     */
    @Override
    public District selectDistrictById(Long id) {
        return districtMapper.selectDistrictById(id);
    }

    /**
     * 查询区域列表
     *
     * @param district 区域
     * @return 区域
     */
    @Override
    public List<District> selectDistrictList(District district) {
        return districtMapper.selectDistrictList(district);
    }

    @Override
    public List<Search> selectDistrict() {
        DistrictType districtType = new DistrictType();
        List<DistrictType> districtTypes = districtTypeMapper.selectDistrictTypeList(districtType);
        District district = new District();
        List<District> districts = districtMapper.selectDistrictList(district);
        List<Search> searches = new ArrayList<>();
        for (DistrictType type: districtTypes) {
            Search search = new Search();
            ArrayList<Children> childrenArray = new ArrayList<>();
            for (District dist:districts) {
                Children children = new Children();
                if (dist.getDistrictTypeId().equals(type.getId())) {
                    children.setLabel(dist.getDistrict());
                    children.setValue(new BigDecimal[]{dist.getLongitude(),dist.getLatitude()});
                    childrenArray.add(children);
                    search.setChildren(childrenArray);
                    search.setLabel(type.getDistrictName());
                    search.setValue(String.valueOf(type.getId()));
                }
            }
            searches.add(search);
        }

        return searches;
    }

    /**
     * 新增区域
     *
     * @param district 区域
     * @return 结果
     */
    @Override
    public int insertDistrict(District district) {
        return districtMapper.insertDistrict(district);
    }

    /**
     * 修改区域
     *
     * @param district 区域
     * @return 结果
     */
    @Override
    public int updateDistrict(District district) {
        return districtMapper.updateDistrict(district);
    }

    /**
     * 批量删除区域
     *
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteDistrictByIds(Long[] ids) {
        return districtMapper.deleteDistrictByIds(ids);
    }

    /**
     * 删除区域信息
     *
     * @param id 区域主键
     * @return 结果
     */
    @Override
    public int deleteDistrictById(Long id) {
        return districtMapper.deleteDistrictById(id);
    }
}
